<template>
  <el-dialog
    title="添加用户"
    :visible.sync="addDialogVisible"
    width="50%"
    @close="addDialogClose"
  >
    <!-- 主体 -->
    <el-form
      :model="addForm"
      status-icon
      :rules="addFormRules"
      label-width="70px"
      ref="addFormRef"
      hide-required-asterisk
    >
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model="addForm.roleName"></el-input>
      </el-form-item>
      <el-form-item label="角色描述">
        <el-input v-model="addForm.roleDesc"></el-input>
      </el-form-item>
    </el-form>
    <!-- 底边按钮区 -->
    <span slot="footer" class="dialog-footer">
      <el-button @click="addDialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="addRoler">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      addDialogVisible: false,
      addForm: {
        roleName: '',
        roleDesc: '',
      },
      addFormRules:{
        roleName:[{
          required: true, message: '请输入角色名', trigger: 'blur'
        }]
      }
    }
  },
  methods: {
    addDialogClose() {
      this.$refs.addFormRef.resetFields()
    },
    addRoler() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return
        //可以发起添加用户请求了
        const { data: res } = await this.$http.post('roles', this.addForm)
        if(res.meta.status !== 201){
            this.$message.error('添加用户失败')
        }
        this.$message.success('添加用户成功！')
        this.addDialogVisible = false
        this.$emit('getRoleList')
      })
    },
  },
}
</script>

<style lang="less" scoped>
.el-form-item {
  padding-right: 5px;
}
</style>